package com.rytx.web.controller.ysp;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.rytx.common.core.domain.model.ArtUser;
import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.system.domain.vo.ArtUserVo;
import com.rytx.system.domain.ysp.ArtOrder;
import com.rytx.system.service.IArtUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.enums.BusinessType;
import com.rytx.system.domain.ysp.ArtMessage;
import com.rytx.system.service.IArtMessageService;
import com.rytx.common.utils.poi.ExcelUtil;
import com.rytx.common.core.page.TableDataInfo;

/**
 * 消息Controller
 *
 * @author rytx
 * @date 2024-05-14
 */
@RestController
@RequestMapping("/system/message")
@Api(value = "消息管理", tags = {"消息管理"})
public class ArtMessageController extends BaseController
{
    @Autowired
    private IArtMessageService artMessageService;
    @Autowired
    private IArtUserService artUserService;
    /**
     * 查询消息列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ArtMessage artMessage)
    {
        startPage();
        List<ArtMessage> list = artMessageService.selectArtMessageList(artMessage);
        List<Map<String,Object>> listMap = new ArrayList<>();

        for(ArtMessage order:list){
            Map<String,Object> map = new HashMap<>();
            map.put("message",order);
            map.put("user",artMessageService.selectArtUserById(order.getReceiver()));
            listMap.add(map);
        }
        return getDataTable(listMap);
    }

    @GetMapping("/app/list")
    @ApiOperation(value = "分页查询消息列表", notes = "分页查询消息列表，传递分页参数")
    public TableDataInfo appList(ArtMessage artMessage)
    {
        LoginUser loginUser = getLoginUser();
        artMessage.setReceiver(loginUser.getArtUser().getId());
        startPage();
        List<ArtMessage> list = artMessageService.selectArtMessageList(artMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArtMessage artMessage)
    {
        List<ArtMessage> list = artMessageService.selectArtMessageList(artMessage);
        ExcelUtil<ArtMessage> util = new ExcelUtil<ArtMessage>(ArtMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "消息详情", notes = "消息详情")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(artMessageService.selectArtMessageById(id));
    }

    /**
     * 新增消息
     */
//    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArtMessage artMessage)
    {
        LoginUser loginUser = getLoginUser();
        artMessage.setStatus("0");
        artMessage.setSender(loginUser.getArtUser().getId());
        artMessage.setSendTime(new Date());
        return toAjax(artMessageService.insertArtMessage(artMessage));
    }

    /**
     * 修改消息
     */

    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArtMessage artMessage)
    {
        artMessage.setReadTime(new Date());
        return toAjax(artMessageService.updateArtMessage(artMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(artMessageService.deleteArtMessageByIds(ids));
    }



}
